package com.spi.soconversion.jmsqueue;



import java.util.Hashtable;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.ConversionType;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.ResourceProvider;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCFileWriter;
import com.spi.soconversion.util.SOCUtility;

public class JMSQueue {

	private Hashtable<String,Object> d_env = new Hashtable<String,Object>();
	private String  d_queueName;
	private Context  d_jndiContext;
	private String d_logFileName;
	private SOCFileWriter d_fileWriter;	
	private QueueConnectionFactory  d_queueConnectionFactory;
	private javax.jms.Queue  d_queue;
	private QueueConnection  d_queueConnection;
	ResourceProvider provider = ResourceProvider.getInstance();
	
	public JMSQueue(String conversionType,String logFileName, SOCFileWriter fileWriter)
	{
		d_logFileName = logFileName;
		d_fileWriter = fileWriter;
		
		if(conversionType.equals(ConversionType.INSTALLBASE.getDisplayText())) {
			d_queueName=provider.getIbQueueName();
		} else if(conversionType.equals(ConversionType.CLOSEDSALESORDER.getDisplayText())) {
			d_queueName=provider.getCsoQueueName();
		} else if(conversionType.equals(ConversionType.OPEN_SALES_ORDER.getDisplayText())) {
			d_queueName=provider.getOsoQueueName();
		}
	
		d_env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
		d_env.put(Context.PROVIDER_URL, provider.getJmsProviderURL());
		d_env.put("java.naming.corba.orb",org.omg.CORBA.ORB.init((String[])null,null));
	}

	public void getJMSClient() throws SOCException
	{
		createJNDIContext();
		getQueueConnectionFactory();
		getQueue();
		getQueueConnection();
	}
	
	private void createJNDIContext() throws SOCException
	{
		try {
			d_jndiContext = new InitialContext(d_env);
			
		} 
		catch (NamingException ne) {

			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
					ErrorConstants.JNDI_CONTEXT_FAILED)+ne.toString()));
			throw new SOCException(
					ErrorMessageReader.getError(
							ErrorConstants.JNDI_CONTEXT_FAILED)+ne.toString());
		}
	}

	private void getQueueConnectionFactory()throws SOCException
	{

		try {
			d_queueConnectionFactory = (QueueConnectionFactory) d_jndiContext.lookup(provider.getQueueConnectionFactory());
		} catch (NamingException ne) {
			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
					ErrorConstants.JNDI_LOOKUP_FAILED_QUEUE_CONNECTION_FACTORY)+provider.getQueueConnectionFactory()+SOCConstants.NEW_LINE+ne.toString()));
			throw new SOCException(
					ErrorMessageReader.getError(
							ErrorConstants.JNDI_LOOKUP_FAILED_QUEUE_CONNECTION_FACTORY)+provider.getQueueConnectionFactory()+SOCConstants.NEW_LINE+ne.toString());
		}
	}
	
	private void getQueue()throws SOCException
	{
		try {
			d_queue = ( javax.jms.Queue) d_jndiContext.lookup(d_queueName);
		} catch (NamingException ne) {
			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
					ErrorConstants.JNDI_LOOKUP_FAILED_QUEUE)+d_queueName+SOCConstants.NEW_LINE+ne.toString()));
			throw new SOCException(
					ErrorMessageReader.getError(
							ErrorConstants.JNDI_LOOKUP_FAILED_QUEUE)+d_queueName+SOCConstants.NEW_LINE+ne.toString());
		}
	}
	
	private void getQueueConnection()throws SOCException
	{
		try {
			d_queueConnection = d_queueConnectionFactory.createQueueConnection();			
		} catch (JMSException jme) {
			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
					ErrorConstants.JMS_QUEUE_CONNECTION_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString()));
			throw new SOCException(
					ErrorMessageReader.getError(
							ErrorConstants.JMS_QUEUE_CONNECTION_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString());
		}
	}
	
	public void pushToQueue(List<String> messageList) throws SOCException
	{
		if(d_queueConnection!=null && d_queue!=null)			
		{
			QueueSession queueSession = null;;
			try {
				queueSession = d_queueConnection.createQueueSession(true,-1);
			} catch (JMSException jme) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.JMS_QUEUE_SESSION_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.JMS_QUEUE_SESSION_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString());
			}
		
			TextMessage   xmlMessage = null;
			QueueSender queueSender = null;
			try {
				queueSender = queueSession.createSender(d_queue);
			} catch (JMSException jme) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.JMS_QUEUE_SENDER_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.JMS_QUEUE_SENDER_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString());
			}
			
			try {
				xmlMessage = queueSession.createTextMessage();
				for (String message: messageList) 
				{
					xmlMessage.setText(message);
					//System.out.println("Thread Name: " + Thread.currentThread().getName());
					queueSender.send(xmlMessage);
				}
			} catch (JMSException jme) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.JMS_QUEUE_MESSAGE_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.JMS_QUEUE_MESSAGE_ERROR)+d_queueName+SOCConstants.NEW_LINE+jme.toString());
			}
			
			try {
				queueSession.commit();
			} catch (JMSException jme) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.JMS_QUEUE_SESSION_COMMIT)+d_queueName+SOCConstants.NEW_LINE+jme.toString()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.JMS_QUEUE_SESSION_COMMIT)+d_queueName+SOCConstants.NEW_LINE+jme.toString());
			}
			
		}
	}
	
}
